import {
  useEffect,
  useState,
  type DependencyList,
  type EffectCallback,
} from 'react'
import {
  useThrottleFn,
  type ThrottleOptions,
} from './use-throttle-fn'
import { useUpdateEffect } from './use-update-effect'

export function useThrottleEffect(
  effect: EffectCallback,
  deps?: DependencyList,
  options?: ThrottleOptions,
): void {
  const [flag, setFlag] = useState({})

  const { run } = useThrottleFn(() => {
    setFlag({})
  }, options)

  useEffect(() => run(), deps)

  useUpdateEffect(effect, [flag])
}
